package com.ego.product.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ego.common.vo.PageVo;
import com.ego.product.po.Attr;
import com.ego.product.service.AttrGroupRelationService;
import com.ego.product.service.AttrService;
import com.ego.product.service.CategoryService;
import com.ego.product.vo.AttrGroupRelationVo;
import com.ego.product.vo.AttrGroupSearchVo;
import com.ego.product.vo.AttrGroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ego.product.po.AttrGroup;
import com.ego.product.service.AttrGroupService;
import com.ego.common.util.Result;

import javax.validation.Valid;

/**
 * @author KamTang
 * @program: ego
 * @description 属性分组控制器
 * @date 2022-11-13 12:56:14
 */
@RestController
@RequestMapping("product/attr-group")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrGroupRelationService attrGroupRelationService;

    /**
     * 获取分类下所有分组以及分组下所有的关联属性
     */
    @GetMapping("/{categoryId}/with-attr")
    public Result getGroupWithAttr(@PathVariable("categoryId") Long categoryId) {
        List<AttrGroup> attrGroups = attrGroupService.list(new QueryWrapper<AttrGroup>().eq("category_id", categoryId));
        List<AttrGroupWithAttrVo> vos = attrGroups.stream().map(group -> {
            AttrGroupWithAttrVo vo = group.toVo();
            List<Long> groupIds = new ArrayList<>();
            groupIds.add(vo.getAttrGroupId());
            List<Attr> attrs = attrService.getByGroupIds(groupIds);
            vo.setAttrs(Optional.ofNullable(attrs).orElse(new ArrayList<>()));
            return vo;
        }).collect(Collectors.toList());
        return Result.ok().put("data", vos);
    }

    /**
     * 列表
     */
    @PostMapping("/search")
    public Result search(@Valid @RequestBody AttrGroupSearchVo search) {
        PageVo<AttrGroup> page = attrGroupService.search(search);
        return Result.ok().put("page", page);
    }


    @GetMapping("/{attrGroupId}/attr/relation")
    private Result attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {
        List<Long> groupIds = new ArrayList<>();
        groupIds.add(attrGroupId);
        List<Attr> entities = attrService.getAttrRelation(groupIds);
        return Result.ok().put("data", entities);
    }

    @PostMapping("/attr/noRelation")
    private Result noRelationAttr(@Valid @RequestBody AttrGroupRelationVo vo) {
        PageVo<Attr> page = attrService.getNoRelationAttr(vo);
        return Result.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/get/{attrGroupId}")
    public Result get(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroup attrGroup = attrGroupService.getById(attrGroupId);
        Long[] path = categoryService.getCategoryPath(attrGroup.getCategoryId());
        attrGroup.setCategoryPath(path);
        return Result.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody AttrGroup attrGroup) {
        attrGroupService.save(attrGroup);
        return Result.ok();
    }

    /**
     * 新增关联关系
     */
    @PostMapping("/attr/relation")
    public Result addRelation(@Valid @RequestBody List<AttrGroupRelationVo> vos) {
        attrGroupRelationService.saveBatch(vos);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@RequestBody AttrGroup attrGroup) {
        attrGroupService.updateById(attrGroup);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return Result.ok();
    }

    /**
     * 关联删除
     */
    @PostMapping("/attr/relation/delete")
    public Result deleteRelations(@Valid @RequestBody AttrGroupRelationVo[] vos) {
        attrGroupService.deleteRelations(vos);
        return Result.ok();
    }

}
